<template>
  <div class="warpper-box payOrder">
    <enterprise-select v-if="userInfo.type == 4" ref="enterprise"
      @enterpriseChange="enterpriseChange"></enterprise-select>
    <a-card style="margin-top: 10px;">
      <a-form layout="inline">
        <a-form-item label="订单编号">
          <a-input placeholder="请输入订单编号" v-model="params.insureOrderId"></a-input>
        </a-form-item>
        <a-form-item label="订单名称">
          <a-input placeholder="请输入订单名称" v-model="params.orderName"></a-input>
        </a-form-item>
        <a-form-item label="订单状态">
          <a-select placeholder="请选择订单状态" v-model="params.orderStatus" style="width: 200px;">
            <a-select-option key="1">
              待提交
            </a-select-option>
            <a-select-option key="2">
              已提交
            </a-select-option>
            <a-select-option key="3">
              已支付
            </a-select-option>
            <a-select-option key="4">
              已拒绝
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="参保月份">
          <a-month-picker valueFormat="YYYY-MM" v-model="params.insureMonth" placeholder="选择月份"></a-month-picker>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" icon="search" @click="loadTable(true)">搜索</a-button>
          <a-button type="primary" icon="setting" @click="reset()">重置</a-button>
          <a-button type="primary" icon="plus" @click="$refs.createInfo.add(enterpriseId)">生成社保订单</a-button>
          <a-button type="primary" @click="downloadOrder">导出</a-button>
        </a-form-item>
      </a-form>
      <a-tabs v-model="params.orderType" @tabClick="orderTypeChange" type="card" style="margin-top: 20px;">
        <a-tab-pane key="1" tab="正常订单"></a-tab-pane>
        <a-tab-pane key="2" tab="补缴订单"></a-tab-pane>
        <a-tab-pane key="3" tab="列账订单"></a-tab-pane>
      </a-tabs>
      <a-table :columns="table.columns" :data-source="table.loadData" :pagination="table.pagination"
        @change="handleTableChange" rowKey="insureOrderId" style="margin-top: 0;">
        <!-- 操作栏-->
        <span slot="action" slot-scope="text, record">
          <!-- 编辑模块 | 字段 -->
          <a @click="goDetail(record)">详情</a>
          <a-divider type="vertical" v-show="record.orderStatus == 3" />
          <a @click="$refs.createInfo.add(enterpriseId,record.insureOrderId)" v-show="record.orderStatus == 3">复制创建</a>
          <a-divider type="vertical" v-show="record.orderStatus == 1" />
          <a @click="submitOrder(record)" v-show="record.orderStatus == 1">提交订单</a>
          <a-divider type="vertical" v-show="record.orderStatus == 1 || params.orderType == 2" />
          <a-popconfirm title="确认删除?" ok-text="确认" cancel-text="取消"
            v-show="record.orderStatus == 1 || params.orderType == 2" @confirm="remove(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </a-card>
    <add-info ref="createInfo" @on-ok="addLoad"></add-info>
  </div>
</template>

<script>
import store from '@/store'
import EnterpriseSelect from "@/components/human/enterpriseSelect.vue"
import {
  COLUMNS_PAYORDER
} from "./form"
import {
  OPTION_BLOCK,
  OPTION_INLINE
} from "../base_form"
import {
  cityJson
} from "@/api/human/city.js"
import AddInfo from "./parOrder_add.vue"
import {
  getTablePage,
  exportInsureOrder,
  submitInsureOrder,
  deleteInsureOrder
} from "@/api/human/ensurePayOrder"
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'payOrder',
  components: {
    AddInfo,
    EnterpriseSelect
  },
  data() {
    return {
      params: {
        insureOrderId: "",
        orderName: "",
        bizType: "",
        orderStatus: undefined,
        insureMonth: "",
        orderType: "1",
      },
      city: [],
      cityJson: cityJson,
      table: {
        columns: COLUMNS_PAYORDER,
        loadData: [],
        pagination: {
          current: 1,
          pageSize: 10,
          total: 0,
          showSizeChanger: true,
					showQuickJumper: true,
          pageSizeOptions: ["10", "20", "50", "100"],
          showTotal: total => `共有 ${total} 条数据`,
        }
      },
      pagination: { current: 1, pageSize: 10 },

      enterpriseId: "", // 企业id
      enterpriseData: [], // 客户列表

      selectedRowKeys: [], // 选择的table数据id
      selectedRows: [], // 选择的table数据
    }
  },
  filters: {

  },
  mounted() {
    window.addEventListener('keyup', this.keyDown);
    if (this.userInfo.type == 4) {
      this.$refs.enterprise.getEnterpriseData();
    } else {
      this.enterpriseId = this.userInfo.relationId
      this.loadTable(true);
    }
  },
  destroyed() {
    window.removeEventListener('keyup', this.keyDown);
  },
  beforeCreate() {
    this.events_form = this.$form.createForm(this, {
      name: 'events_form'
    })
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {

  },
  methods: {
    // table复选框选择
    onSelectChange(selectedRowKeys, selectedRows) {
      // console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
    },
    // 获取列表
    async loadTable(flag) {
      let params = this.params;
      if (flag) {
        this.table.pagination.current = 1;
        this.table.pagination.pageSize = 10;
      }
      if (this.city.length > 0) {
        this.city.pop();
        params.city = this.city.join(',');
      }
      params.enterpriseId = this.enterpriseId;
      params.pageNum = this.table.pagination.current;
      params.pageSize = this.table.pagination.pageSize;
      const { code, data } = await getTablePage(params);
      if (code === '1') {
        this.table.loadData = data.content;
        this.table.pagination.total = data.total || 0;
      }
    },
    handleTableChange(pagination) {
      this.table.pagination.current = pagination.current;
      this.table.pagination.pageSize = pagination.pageSize;
      this.loadTable(false);
    },
    // 导出订单
    downloadOrder() {
      exportInsureOrder({ 
        enterpriseId: this.enterpriseId, 
        orderType: this.params.orderType, 
        insureOrderId: this.params.insureOrderId 
      }).then(res => {
        const blob = new Blob([res]);
        const fileName = '参保人员订单.xlsx';
        const elink = document.createElement('a'); // 创建a标签
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click(); // 点击下载
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink); // 释放标签
      })
    },
    // 订单类型切换
    orderTypeChange(e) {
      this.loadTable(true)
    },
    // 提交订单
    async submitOrder(row) {
      let params = {
        insureOrderId: row.insureOrderId
      }
      const { code, data } = await submitInsureOrder(params);
      if (code == "1") {
        this.$message.success('操作成功');
        this.loadTable(true);
      }
    },
    // 删除订单
    async remove(row) {
      let params = {
        insureOrderId: row.insureOrderId,
      }
      const { code } = await deleteInsureOrder(params)
      if (code == 1) {
        this.$message.success('操作成功');
        this.loadTable(true);
      }
    },
    // 跳转订单详情
    goDetail(row) {
      this.$router.push({ name: 'humanResourcesEnsurePayOrderDetailCode', query: { insureOrderId: row.insureOrderId, orderStatus: row.orderStatus, orderType: this.params.orderType, enterpriseId: this.enterpriseId } });
    },
    // 企业选择回调
    enterpriseChange(value) {
      this.enterpriseId = value;
      this.loadTable(true);
    },
    addLoad() {
      this.loadTable(true);
      this.selectedRowKeys = [];
      this.selectedRows = [];
    },
    reset() {
      this.params.insureOrderId = "";
      this.params.orderName = "";
      this.params.orderStatus = undefined;
      this.params.insureMonth = "";
      this.params.bizType = "";
      this.loadTable(true);
    }
  }
}
</script>
<style lang="less">
.payOrder {
  .ant-tabs-bar {
    margin-bottom: 5px;
  }
}</style>